package cn.ctbu.xiaopeng.studentscoremanage.config;

import cn.ctbu.xiaopeng.studentscoremanage.dao.UserRepository;
import cn.ctbu.xiaopeng.studentscoremanage.interceptor.LoginInterceptor;
import cn.ctbu.xiaopeng.studentscoremanage.interceptor.UserTypeInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author xiaopeng
 * @version 1.0
 */
@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

    private final UserRepository userRepository;

    /**
     * 配置静态资源路径
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源路径 "/static/**"
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/"); // 指向 src/main/resources/static/

     /*   // 如果需要其他路径，比如 "/templates/**"
        registry.addResourceHandler("/templates/**")
                .addResourceLocations("classpath:/templates/"); // 指向 src/main/resources/templates/*/
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**") // 拦截所有请求
                //不拦截的路径
                .excludePathPatterns("/login.html", // 排除登录请求
//                        "/api/student/validate", //登录校验接口
                        "/api/user/validate", //登录校验接口
                        "/admin/**", "/component/**", "/config/**", "/static/**",
                        "/css/**", "/view/**");  //静态资源路径

        // 添加权限拦截器
        registry.addInterceptor(new UserTypeInterceptor(userRepository))
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns("/login.html", // 排除登录请求
//                        "/api/student/validate", //登录校验接口
                        "/api/user/validate", //登录校验接口
                        "/admin/**", "/component/**", "/config/**", "/static/**",
                        "/css/**", "/view/**"); // 排除登录和登出接口
    }

}
